package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.model.LatLng;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersPosition3Binding;
import com.sgb.kjwl.view.widget.map.MapNavigationDialog3;
import com.sgb.kjwl.view.widget.map.PackerMapInfoPop3;
import com.swgk.core.base.di.AppComponent;

/**
 * @author 毁灭霸者
 * @date 2020/7/21
 * description:地图定位
 */
@Deprecated
public class PackersPositionActivity3 extends AppActivity {
    private ActivityPackersPosition3Binding binding;

    private Double currentLat;
    private Double currentLong;

    private final String startAddress = "";
    private String endAddress = "";
    private PackerMapInfoPop3 packerMapInfoPop3;
    private String address;
    private LatLng positionLatLng;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, PackersPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.setClass(context, PackersPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, Double longitude, Double latitude, String address) {
        Intent intent = new Intent();
        intent.putExtra("longitude", longitude);
        intent.putExtra("latitude", latitude);
        intent.putExtra("address", address);
        intent.setClass(context, PackersPositionActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initMap(savedInstanceState);
        initPoiSearch();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PackersPositionActivity3.this, R.layout.activity_packers_position3);
        binding.include.backView.setVisibility(View.VISIBLE);
        binding.include.backView.setOnClickListener(this);
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.titleView.setText("地图定位");
    }

    @Override
    protected void init() {
        currentLong = getIntent().getDoubleExtra("longitude", 0);
        currentLat = getIntent().getDoubleExtra("latitude", 0);
        endAddress = getIntent().getStringExtra("address");

        packerMapInfoPop3 = new PackerMapInfoPop3(PackersPositionActivity3.this);
        packerMapInfoPop3.setFocusable(false);
        packerMapInfoPop3.setOutsideTouchable(false);
        packerMapInfoPop3.setPopCallBack(this::goNavigation);
        packerMapInfoPop3.setEndData(endAddress);
    }

    private void initMap(Bundle savedInstanceState) {
        binding.mMapView.initMap(savedInstanceState, getLifecycle(), new MapMarkerView.CallBack() {
            @Override
            public void onMarkerClick(int pos, String title) {
                showMarkerView();
            }

            @Override
            public void onLocation(Double lat, Double log, String string) {
                //详细地址
                address = string;
                positionLatLng = new LatLng(currentLat, currentLong);
                if (!TextUtils.isEmpty(address)) {
                    binding.include.titleView.setText(address);
                    packerMapInfoPop3.setStartData(address);
                }
            }

            @Override
            public void getAddress(Double lat, Double log, String string) {
                super.getAddress(lat, log, address);
                address = string;
                positionLatLng = new LatLng(currentLat, currentLong);
            }

            @Override
            public void onRegeocodeSearched(Double lat, Double log, String string) {
                super.onRegeocodeSearched(lat, log, address);
                //详细地址
                address = string;
                if (!TextUtils.isEmpty(address)) {
                    binding.include.titleView.setText(address);
                    packerMapInfoPop3.setStartData(address);
                }
            }
        });
        binding.mMapView.setMapClick();
    }

    public void showMarkerView() {
        packerMapInfoPop3.showPopwindowBottom(binding.mMapView);
    }

    private void initPoiSearch() {
        if (currentLat != 0 && currentLong != 0) {
            binding.mMapView.setMyLocation(currentLat, currentLong, R.mipmap.parcker_map_position);
            binding.mMapView.getLatLotExtra(currentLat, currentLong);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_view) {
            finish();
        }
    }

    private void goNavigation() {
        new MapNavigationDialog3(PackersPositionActivity3.this, null)
                .setDialogClickCallBack(new MapNavigationDialog3.DialogClickCallBack() {
                    @Override
                    public void onClickBaiDu() {
                        if (!isAvilible(PackersPositionActivity3.this, "com.baidu.BaiduMap")) {
                            Toast.makeText(PackersPositionActivity3.this, "该手机没安装百度地图", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        jumpBaiDuMap(positionLatLng.latitude, positionLatLng.longitude);
                    }

                    @Override
                    public void onClickGaoDe() {
                        if (!isAvilible(PackersPositionActivity3.this, "com.autonavi.minimap")) {
                            Toast.makeText(PackersPositionActivity3.this, "该手机没安装高德地图", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        jumpGaoDeMap(positionLatLng.latitude, positionLatLng.longitude, endAddress);
                    }

                    @Override
                    public void onClickTenXun() {
                        if (!isAvilible(PackersPositionActivity3.this, "com.tencent.map")) {
                            Toast.makeText(PackersPositionActivity3.this, "该手机没安装腾讯地图", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        positionLatLng = new LatLng(currentLat, currentLong);
                        jumpTenXunMap(positionLatLng.latitude, positionLatLng.longitude, startAddress, currentLat, currentLong, endAddress);
                    }
                }).show();
    }
}
